package com.memory.admin.earn.controller;

import com.memory.common.utils.ResultVoUtil;
import com.memory.common.vo.ResultVo;
import com.memory.component.actionLog.annotation.EntityParam;
import com.memory.modules.business.domain.earn.AccountEarnRecord;
import com.memory.modules.business.service.earn.AccountEarnRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @PackageName: com.memory.admin.earn.controller
 * @ClassName: AccountEarnRecordController
 * @Description:
 * @author: zhuguangxun
 * @date 2021/3/14
 */
@Controller
@RequestMapping("/business/account/earn/record")
@RequiredArgsConstructor
public class AccountEarnRecordController {

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


    private final static String PATH = "/business/accountearnrecord/";

    private final AccountEarnRecordService accountEarnRecordService;

    @GetMapping("list/{id}")
    public String list(@PathVariable("id") Long accountId, Model model) {
        ExampleMatcher matcher = ExampleMatcher.matching();
        AccountEarnRecord record = new AccountEarnRecord();
        record.setAccountId(accountId);
        Example<AccountEarnRecord> example = Example.of(record, matcher);
        final Page<AccountEarnRecord> page = accountEarnRecordService.findPage(example);
        model.addAttribute("accountId",accountId);
        model.addAttribute("page", page);
        model.addAttribute("list", page.getContent());
        return PATH + "list";
    }

    @GetMapping("add/{id}")
    public String add(@PathVariable("id") Long accountId,Model model) {
        model.addAttribute("accountId",accountId);
        return PATH + "add";
    }


    @PostMapping("save")
    public @ResponseBody
    ResultVo save(@EntityParam AccountEarnRecord record) {
        if (record.getId() == null) {
            accountEarnRecordService.create(record);
        } else {

        }
        return ResultVoUtil.SAVE_SUCCESS;
    }
}
